import pymysql
from django.shortcuts import render
import json


# Create your views here.

def home(request):
    import requests
    database = pymysql.connect(host="localhost", user="root", password="zzh200086", db="dachuang")
    cursor = database.cursor()
    sql1 = "select longitude,latitude,weight,center_name from center"
    sql2 = "select longitude,latitude,weight,dst_name from destination"

    cursor.execute(sql1)
    sql1_res = cursor.fetchall()

    cursor.execute(sql2)
    sql2_res = cursor.fetchall()

    ct_x = []
    for sql_info in sql1_res:
        ct_x.append({'name': sql_info[3], 'longitude': sql_info[0], 'latitude': sql_info[1], 'weight': sql_info[2]})

    dst_x = []
    for sql_info in sql2_res:
        dst_x.append({'name': sql_info[3], 'longitude': sql_info[0], 'latitude': sql_info[1], 'weight': sql_info[2]})

    print(dst_x)

    return render(request, 'home.html', {'center': ct_x, 'dst': dst_x})
